package com.fractalist.base.core.utils;

/**
 * 产生16位随机图片名称
 */
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class IPTimeStamp {
	private Calendar cal = null;
	private String ip;

	public IPTimeStamp() {
		cal = new GregorianCalendar();
	}

	/**
	 * ����IP��ַ��IP��ַ��ʱ������
	 * 
	 * @param ip
	 */
	public IPTimeStamp(String ip) {
		this();

		if (ip.startsWith("0:0:0:0:0")) {
			ip = "127.0.0.1";
		}
		this.ip = ip;
	}

	public String getTimeStamp() {
		String ts = this.addZero(this.cal.get(Calendar.YEAR) + "", 4)
				+ this.addZero((this.cal.get(Calendar.MONTH) + 1) + "", 2)
				+ this.addZero(this.cal.get(Calendar.DAY_OF_MONTH) + "", 2)
				+ this.addZero(this.cal.get(Calendar.HOUR_OF_DAY) + "", 2)
				+ this.addZero(this.cal.get(Calendar.MINUTE) + "", 2)
				+ this.addZero(this.cal.get(Calendar.SECOND) + "", 2)
				+ this.addZero(this.cal.get(Calendar.MILLISECOND) + "", 3);
		return ts;
	}

	/**
	 * 
	 * @param temp
	 *            ������������
	 * @param len
	 *            ���
	 * @return ���õ�����
	 */
	private String addZero(String temp, int len) {
		while (temp.length() < len) {
			temp = "0" + temp;
		}
		return temp;
	}

	public String getIpStamp() {
		String is = null;
		String temp[] = this.ip.split("\\.");
		is = this.addZero(temp[0], 3) + this.addZero(temp[1], 3)
				+ this.addZero(temp[2], 3) + this.addZero(temp[3], 3);
		return is;
	}

	// �õ�IP+ʱ���
	public String getIPTimeStamp() {
		return this.getIpStamp() + this.getTimeStamp();
	}

	/**
	 * 
	 * @retuص�ǰʱ�䣬��ʽ��yy-mm-dd hh:mm:ss:mmm
	 */
	// ���Ҫ�ȵ���ǰʱ��
	public String getCurrentDate() {
		String time = null;
		time = this.cal.get(Calendar.YEAR) + ""
				+ (this.cal.get(Calendar.MONTH) + 1)
				+ this.cal.get(Calendar.DAY_OF_MONTH)
				+ this.cal.get(Calendar.HOUR_OF_DAY)
				+ this.cal.get(Calendar.MINUTE) + this.cal.get(Calendar.SECOND)
				+ this.cal.get(Calendar.MILLISECOND);
		return time;
	}

	/**
	 * 
	 * @return ���ص�ǰʱ�䣬��ʽ��yyyy��mm��dd�� hhʱmm��ss��
	 */
	public String getChinaCurrentDate() {
		// Ҫʵ�ִ˹��ܣ���Ҫʹ��SimpleDateFormat��
		// ��ʽת����
		/*
		 * SimpleDateFormat sdf1 = null ; sdf1 = new
		 * SimpleDateFormat("yyyy-MM-dd kk:mm:ss:SSS") ;
		 */
		SimpleDateFormat sdf2 = null;
		sdf2 = new SimpleDateFormat("yyyy��MM��dd�� kkʱmm��ss��SSS");
		/*
		 * Date d = null ; try { d = sdf1.parse(this.getCurrentDate()); } catch
		 * (ParseException e) { // TODO �Զ���� catch �� e.printStackTrace(); }
		 */
		String result = sdf2.format(new Date());
		return result;
	}

	public static void main(String args[]) {
		System.out.print(new IPTimeStamp().getTimeStamp());
	}
}
